﻿package com.wandian.util
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	/**   
	 * @author long
	 * @QQ 108232706
	 * @version 1.0   
	 */ 
	public class CLZTabBar extends EventDispatcher
	{
		private var arrTabBar:Array;
		private static const UP:uint=1;
		private static const OVER:uint=2;
		private static const DOWN:uint=3;

		public function CLZTabBar(tabs:Array):void
		{
			if (tabs.length <= 0)
			{
				return;
			}
			this.arrTabBar=tabs;
			this.checkListener();
			return;
		}

		public function setClickIndex(tab:MovieClip):void
		{
			this.clickThis(tab);
			return;
		}

		private function checkListener():void
		{
			var mcTab:MovieClip=null;
			var arrColor:Array=null;
			var count:int=0;
			while (count < this.arrTabBar.length)
			{

				mcTab=this.arrTabBar[count][0] as MovieClip;
				if (mcTab.hasEventListener(MouseEvent.CLICK) == false)
				{
					mcTab.mouseChildren=false;
					mcTab.buttonMode=true;
					mcTab.gotoAndStop(1);
					if (this.arrTabBar[count][3] != null)
					{
						arrColor=this.arrTabBar[count][3].split(",");
						mcTab.arrColor=arrColor;
					}
					if (this.arrTabBar[count][1] != null)
					{
						mcTab.txtMsg=this.arrTabBar[count][1];
					}
					mcTab.addEventListener(MouseEvent.CLICK, this.clickHandler);
					mcTab.addEventListener(MouseEvent.MOUSE_OVER, this.overHandler);
					mcTab.addEventListener(MouseEvent.MOUSE_OUT, this.outHandler);
					mcTab.gotoAndStop(UP);
					updateTxtAndColor(mcTab);
				}
				count=count + 1;
			}
			return;
		}

		private function overHandler(event:MouseEvent):void
		{
			var mcTab:*=event.currentTarget as MovieClip;
			mcTab.gotoAndStop(OVER);
			updateTxtAndColor(mcTab, (OVER - 1));
			return;
		}

		private function outHandler(event:MouseEvent):void
		{
			var mcTab:*=event.currentTarget as MovieClip;
			mcTab.gotoAndStop(UP);
			updateTxtAndColor(mcTab, (UP - 1));
			return;
		}

		private function clickHandler(event:MouseEvent):void
		{
			this.clickThis(event.currentTarget as MovieClip);
			return;
		}

		private function clickThis(mcTab:MovieClip):void
		{
			this.checkListener();
			mcTab.removeEventListener(MouseEvent.CLICK, this.clickHandler);
			mcTab.removeEventListener(MouseEvent.MOUSE_OVER, this.overHandler);
			mcTab.removeEventListener(MouseEvent.MOUSE_OUT, this.outHandler);
			mcTab.gotoAndStop(DOWN);
			updateTxtAndColor(mcTab, (DOWN - 1));
			mcTab.buttonMode=false;
			mcTab.dispatchEvent(new Event(Event.CHANGE));
			return;
		}

		private static function updateTxtAndColor(mc:MovieClip, color:int=0):void
		{
			if (mc.txtMsg != null)
			{
				if (mc.txtMsg != "")
				{
					if (mc.txt != null)
					{
						mc.txt.text=mc.txtMsg;
						if (mc.arrColor != null)
						{
							mc.txt.textColor=mc.arrColor[color];
						}
					}
				}
			}

			return;
		}

	}
}
